import { Controller } from "../../../../mvc/core/Controller";
import { WindowEvent } from "../../../../component/window/WindowEvent";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { PetIslandApplyPanel } from "../PetIslandApplyPanel";
import { ResFileConst } from "../../../resource/ResFileConst";
import { GameProxy } from "../../../mvc/GameProxy";
import { GameController } from "../../../mvc/GameController";
import { DataEvent } from "../../../events/DataEvent";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SRobIslandSignUpInfo } from "../../../../../Message/Game/SRobIslandSignUpInfo";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { SEntityId } from "../../../../../Message/Public/SEntityId";
import { SPetInfo } from "../../../../../Message/Game/SPetInfo";
import { PetIslandApplyInfoPanel } from "../PetIslandApplyInfoPanel";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { IView } from "../../../../mvc/interfaces/IView";
//class PetIslandApplyController
    
    export  class PetIslandApplyController extends Controller
    {
        constructor()
        {
            super();
            return;
        }

        /* internal  */showPanel(arg1: WindowEvent): void
        {
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            return;
        }

        /* internal  */closePanel(arg1: WindowEvent): void
        {
            Dispatcher.removeEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            return;
        }

        /* internal  */onGuildStateChangeHandler(arg1: Object): void
        {
            if (this.applyPanel && !this.applyPanel.isHide) 
            {
                this.applyPanelInfoPanel.petListData = [];
                PetIslandApplyPanel(view).petListLabel = 0;
                PetIslandApplyPanel(view).operateBtnStyleName = ResFileConst.PetIslandApplyJoinGuildBtn;
                GameProxy.petIsland.getSignUpInfo();
            }
            if (GameController.petIsland.mapController.petIslandMapPanel && !GameController.petIsland.mapController.petIslandMapPanel.isHide) 
            {
                GameController.petIsland.mapController.view.hide();
            }
            else if (GameController.petIsland.powerBaseController.petIslandPowerBasePanel && !GameController.petIsland.powerBaseController.petIslandPowerBasePanel.isHide) 
            {
                GameController.petIsland.powerBaseController.view.hide();
            }
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            this.applyPanel.setPetListComBo();
            return;
        }

        /* internal  */petIslandSignUpInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SRobIslandSignUpInfo;
            cache.petIsland.guildApplyInfo = loc1;
            if ((cache.petIsland.needShowView || _view && !_view.isHide) && cache.petIsland.needJumpMapView) 
            {
                if (_view && !_view.isHide) 
                {
                    _view.hide();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetIslandPowerBaseViewShowByIndex, 0));
                cache.petIsland.needShowView = false;
            }
            else 
            {
                if (cache.petIsland.needShowView) 
                {
                    view.show();
                    cache.petIsland.needShowView = false;
                }
                if (_view && !_view.isHide) 
                {
                    if (loc1.signUpSuccess && !cache.petIsland.hasJoin) 
                    {
                        PetIslandApplyPanel(view).operateBtnStyleName = ResFileConst.PetIslandApplyJoinBtn;
                    }
                    else if (cache.petIsland.hasJoin) 
                    {
                        PetIslandApplyPanel(view).operateBtnStyleName = "";
                    }
                    else 
                    {
                        PetIslandApplyPanel(view).operateBtnStyleName = ResFileConst.PetIslandApplyBtn;
                    }
                    PetIslandApplyPanel(view).petListLabel = loc1.players.length;
                    PetIslandApplyPanel(view).applyPet = loc1.petId;
                }
            }
            return;
        }

        /* internal  */onPetIslandApplyInfoShowHandler(arg1: DataEvent): void
        {
            if (cache.guild.myGuildInfo) 
            {
                this.applyPanelInfoPanel.show();
                this.applyPanelInfoPanel.petListData = cache.petIsland.guildApplyInfo.players;
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(28237));
            }
            return;
        }

        /* internal  */onPetIslandGetPetInfoHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.petIsland.getPetInfo(loc1);
            return;
        }

        /* internal  */onPetIslandApplyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.petIsland.join(loc1);
            return;
        }

        /* internal  */petIslandPetInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPetInfo;
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetShowInfo, loc1));
            return;
        }

        /* internal  */get applyPanelInfoPanel(): PetIslandApplyInfoPanel
        {
            if (!this._applyPanelInfoPanel) 
            {
                this._applyPanelInfoPanel = new PetIslandApplyInfoPanel();
            }
            return this._applyPanelInfoPanel;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PetIslandApply, this.onPetIslandApplyHandler);
            Dispatcher.addEventListener(EventName.PetIslandApplyInfoShow, this.onPetIslandApplyInfoShowHandler);
            Dispatcher.addEventListener(EventName.PetIslandGetPetInfo, this.onPetIslandGetPetInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildEnterGuild, this.onGuildStateChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.GuildLeaveGuild, this.onGuildStateChangeHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandSignUpInfo, this.petIslandSignUpInfo);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRobIslandPetInfo, this.petIslandPetInfo);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this.applyPanel) 
            {
                this.applyPanel = new PetIslandApplyPanel();
                this.applyPanel.addEventListener(WindowEvent.SHOW, this.showPanel);
                this.applyPanel.addEventListener(WindowEvent.CLOSE, this.closePanel);
            }
            return this.applyPanel;
        }

        public /* var */applyPanel: PetIslandApplyPanel;

        /* internal */ /* var */_applyPanelInfoPanel: PetIslandApplyInfoPanel;
    }
